﻿// 以下代码由 Microsoft Visual Studio 2005 生成。
// 测试所有者应该检查每个测试的有效性。
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Configuration;

namespace TestMiniAccess
{
    using Ruandao.MiniAccess.OrMap;
    using Ruandao.MiniAccess.SqlServer;
    using Ruandao.MiniAccess;

    [TestClass()]
    public class SqlBuilderTest
    {
        string tableName;
        IList<ColumnMap> columnMaps;

        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }
        #region 附加测试属性

        [TestInitialize()]
        public void MyTestInitialize()
        {
            string connString = ConfigurationManager.ConnectionStrings["TestMiniAccessConnection"].ConnectionString;
            Database database = new SqlDatabase(connString);
            tableName = "ForeignKeyTable";
            columnMaps = new TableMap("ForeignKeyTable", database).ColumnMaps;
        }
        
        //使用 TestCleanup 在运行完每个测试后运行代码
        //
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        [DeploymentItem("Ruandao.MiniDac.dll")]
        [TestMethod()]
        public void BuildDeleteSqlTest()
        {
            string expected =
@"DELETE FROM [ForeignKeyTable]
WHERE
	[CompositeKey1] = @CompositeKey1 AND
	[CompositeKey2] = @CompositeKey2 AND
	[CompositeKey3] = @CompositeKey3
";
            string actual;
            actual = Ruandao.MiniAccess.SqlServer.SqlBuilder.BuildDeleteSql(tableName, columnMaps);

            Assert.AreEqual(expected, actual, "Ruandao.MiniDac.SqlServer.SqlBuilder.BuildDeleteSql 未返回所需的值。");
        }


        [DeploymentItem("Ruandao.MiniDac.dll")]
        [TestMethod()]
        public void BuildDeleteByConditionsSqlTest()
        {
            string expected =
@"DECLARE @N NVarChar(800);
SET @N=N'DELETE
	FROM [ForeignKeyTable] '
	+ @Conditions;
EXECUTE sp_executesql @N
";
            string actual;
            actual = Ruandao.MiniAccess.SqlServer.SqlBuilder.BuildDeleteByConditionsSql(tableName, columnMaps);

            Assert.AreEqual(expected, actual, "Ruandao.MiniDac.SqlServer.SqlBuilder.BuildDeleteSql 未返回所需的值。");
        }
        [DeploymentItem("Ruandao.MiniDac.dll")]
        [TestMethod()]
        public void BuildInsertSqlTest()
        {
            string expected =
@"INSERT INTO [ForeignKeyTable] (
	[CompositeKey1],
	[CompositeKey2],
	[CompositeKey3],
	[PrimaryKeyId],
	[TableId],
	[GuidField],
	[Field1],
	[Field2]
)
VALUES
(
	@CompositeKey1,
	@CompositeKey2,
	@CompositeKey3,
	@PrimaryKeyId,
	@TableId,
	@GuidField,
	@Field1,
	@Field2
)
";
            string actual;
            actual = Ruandao.MiniAccess.SqlServer.SqlBuilder.BuildInsertSql(tableName, columnMaps);

            Assert.AreEqual(expected, actual, "Ruandao.MiniDac.SqlServer.SqlBuilder.BuildInsertSql 未返回所需的值。");
        }

        [DeploymentItem("Ruandao.MiniDac.dll")]
        [TestMethod()]
        public void BuildSelectByConditionsSqlTest()
        {
            string expected =
@"DECLARE @N NVarChar(2000);
SET @N=N'SELECT
	[ForeignKeyTable].[CompositeKey1],
	[ForeignKeyTable].[CompositeKey2],
	[ForeignKeyTable].[CompositeKey3],
	[ForeignKeyTable].[PrimaryKeyId],
	[ForeignKeyTable].[TableId],
	[ForeignKeyTable].[GuidField],
	[ForeignKeyTable].[Field1],
	[ForeignKeyTable].[Field2]
	FROM [ForeignKeyTable] '+ @Conditions;
EXECUTE sp_executesql @N
";
            string actual;

            actual = Ruandao.MiniAccess.SqlServer.SqlBuilder.BuildSelectByConditionsSql(tableName, columnMaps);

            Assert.AreEqual(expected, actual, "Ruandao.MiniDac.SqlServer.SqlBuilder.BuildSelectByConditionSql 未返回所需的值。");
        }

        [DeploymentItem("Ruandao.MiniDac.dll")]
        [TestMethod()]
        public void BuildSelectByKeysSqlTest()
        {
            string expected =
@"SELECT
	[ForeignKeyTable].[CompositeKey1],
	[ForeignKeyTable].[CompositeKey2],
	[ForeignKeyTable].[CompositeKey3],
	[ForeignKeyTable].[PrimaryKeyId],
	[ForeignKeyTable].[TableId],
	[ForeignKeyTable].[GuidField],
	[ForeignKeyTable].[Field1],
	[ForeignKeyTable].[Field2]
FROM [ForeignKeyTable]
WHERE
	[CompositeKey1] = @CompositeKey1 AND
	[CompositeKey2] = @CompositeKey2 AND
	[CompositeKey3] = @CompositeKey3
";
                string actual;

                actual = Ruandao.MiniAccess.SqlServer.SqlBuilder.BuildSelectByKeysSql(tableName, columnMaps);

            Assert.AreEqual(expected, actual, "Ruandao.MiniDac.SqlServer.SqlBuilder.BuildSelectByKeysSql 未返回所需的值。");
        }

        [DeploymentItem("Ruandao.MiniDac.dll")]
        [TestMethod()]
        public void BuildUpdateSqlTest()
        {

            string expected =
@"UPDATE [ForeignKeyTable]
SET
	[CompositeKey1] = @New_CompositeKey1,
	[CompositeKey2] = @New_CompositeKey2,
	[CompositeKey3] = @New_CompositeKey3,
	[PrimaryKeyId] = @PrimaryKeyId,
	[TableId] = @TableId,
	[GuidField] = @GuidField,
	[Field1] = @Field1,
	[Field2] = @Field2
WHERE
	[CompositeKey1] = @CompositeKey1 AND
	[CompositeKey2] = @CompositeKey2 AND
	[CompositeKey3] = @CompositeKey3
";
            string actual;

            actual = Ruandao.MiniAccess.SqlServer.SqlBuilder.BuildUpdateSql(tableName, columnMaps);

            Assert.AreEqual(expected, actual, "Ruandao.MiniDac.SqlServer.SqlBuilder.BuildUpdateSql 未返回所需的值。");
        }

    }


}
